
"""
    pyprosim - a simple prolog compiler developed to PCS2056 course (Compilers) at Poli-USP\
    Copyright (C) 2008 Rodrigo Rodrigues da Silva

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
"""

from pyprosim.token import *
from pycoopiler.enum import enum
from pyprosim.submaquina import submaquina
from pyprosim.maq_dado import maq_dado

class maq_lclausula(submaquina):
    
    states = enum("_0_NOT_OU_ABRE_P _1_ABRE_P _2_INF _3_DADO _4_FECHA_P _5_AND_OU_OR _6_NOT_OU_ABRE_P")
    
    def __init__(self, _tkzr):
        self.tkzr = _tkzr     
        self.state = self.states._0_NOT_OU_ABRE_P
        self.log_chamada("LCLAUSULA")
        self.current = self.tkzr.current
        
    def analyze(self):
        while not (self.state == self.states._5_AND_OU_OR and \
                   not (self.current.type == self.current.types.OP_AND or self.current.type == self.current.types.OP_OR)):
            self.log_state()  
              
            if self.state == self.states._0_NOT_OU_ABRE_P:
                if self.current.type == self.current.types.OP_NOT:
                    self.state = self.states._1_ABRE_P
                elif self.current.type == self.current.types.ABRE_P:
                    self.state = self.states._2_INF
                else: raise Exception ("Erro Sintatico! Esperando OP_NOT ou ABRE_P")
                self.consume()
            
            elif self.state == self.states._1_ABRE_P:
                if self.current.type == self.current.types.ABRE_P:
                    self.state = self.states._2_INF
                else: raise Exception ("Erro Sintatico! Esperando ABRE_P")
                self.consume()
            
            elif self.state == self.states._2_INF:
                if self.current.type == self.current.types.INF:
                    self.state = self.states._3_DADO
                else: raise Exception ("Erro Sintatico! Esperando INF")
                self.consume()
            
            elif self.state == self.states._3_DADO:
                md = maq_dado(self.tkzr)
                md.analyze()       
                self.current = self.tkzr.current
                self.state = self.states._4_FECHA_P
            
            elif self.state == self.states._4_FECHA_P:
                if self.current.type == self.current.types.FECHA_P:
                    self.state = self.states._5_AND_OU_OR
                else: raise Exception ("Erro Sintatico! Esperando FECHA_P")
                self.consume()
                
            elif self.state == self.states._5_AND_OU_OR:
                if self.current.type == self.current.types.OP_AND:
                    pass
                elif self.current.type == self.current.types.OP_OR:
                    pass
                else: raise Exception ("Erro Sintatico! Esperando OP_AND ou OP_OR")
                self.state = self.states._6_NOT_OU_ABRE_P
                self.consume()
                
            elif self.state == self.states._6_NOT_OU_ABRE_P:
                if self.current.type == self.current.types.OP_NOT:
                    self.state = self.states._1_ABRE_P
                elif self.current.type == self.current.types.ABRE_P:
                    self.state = self.states._2_INF
                else: raise Exception ("Erro Sintatico! Esperando NOT ou ABRE_P")
                self.consume()
            
            else: raise Exception("ERRO: Estado invalido: " + self.state)
        
        self.log_retorno("LCLAUSULA")